package com.example.datatransfer.controller;

import com.example.datatransfer.callback.SourceCallback;
import com.example.datatransfer.callback.TargetCallback;
import com.example.datatransfer.entity.TableInfo;
import com.example.datatransfer.jdbc.JdbcConfig;
import com.example.datatransfer.jdbc.JdbcSourceTool;
import com.example.datatransfer.jdbc.JdbcTargetTool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DataTransferController {

    JdbcConfig sourceConfig = new JdbcConfig("jdbc:mysql://localhost:3306/bhy_database",
            "cust","cust");
    JdbcConfig targetConfig = new JdbcConfig("jdbc:mysql://localhost:3306/wx_warning",
            "cust","cust");

    @RequestMapping(value="/index")
    public String index(){
        return "index";
    }

    @PostMapping(value = "/getTableList")
    @ResponseBody
    public List<String> getTableList(){
        List<String> tableList = JdbcSourceTool.getInstance().getTableNameList(sourceConfig);
        return tableList;
    }

    @PostMapping(value="generateTable")
    @ResponseBody
    public List<String> generateTable(String tableName){
        msgList.clear();

        TableInfo tableInfo = JdbcSourceTool.getInstance().getTableInfo(sourceConfig,tableName,sourceCallback);
        //根据源表的信息，在目标库中复制表
        JdbcTargetTool.getInstance().generateTableAndData(targetConfig,tableInfo,targetCallback);
        return msgList;
    }


    List<String> msgList = new ArrayList<>();



    private SourceCallback sourceCallback = new SourceCallback() {
        @Override
        public void onReadAllField(String msg) {
            msgList.add(msg);
        }

        @Override
        public void onMakeTableInfo(String msg) {
            msgList.add(msg);
        }

        @Override
        public void onReadData(String msg) {
            msgList.add(msg);
        }
    };
    private TargetCallback targetCallback = new TargetCallback() {
        @Override
        public void onTableGenerated(String msg) {
            msgList.add(msg);
        }

        @Override
        public void onDataInserted(String msg) {
            msgList.add(msg);
        }
    };



}
